package com.paddyi.config;

import com.paddyi.jwt.JwtAuthenticationTokenFilter;
import com.paddyi.security.MyFailureHandler;
import com.paddyi.security.MySuccessHandler;
import com.paddyi.security.MyUserDetailsService;
import com.paddyi.security.PasswordEncoderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)  //  启用方法级别的权限认证
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private MyUserDetailsService myUserDetailsService;
    @Autowired
    private MySuccessHandler mySuccessHandler;
    @Autowired
    private MyFailureHandler myFailureHandler;
    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;


    //过滤器的配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER) // 禁用session
                .and()
                .headers().frameOptions().disable() //默认禁止iframe页面访问，这里关闭
                .and()
                .authorizeRequests()
                .antMatchers("/assets/**","/", "/login", "/actuator/**","/index","/isLogin","/ssoUser/register","/refreshToken","/signup","/signin")// 允许直接访问的地址
                .permitAll()
                .anyRequest().authenticated()   // 其他地址的访问均需验证权限
                .and()
                .formLogin()
                .loginPage("/login")   //  登录页
                .successHandler(mySuccessHandler)
//                .successForwardUrl("/index")  // 成功后跳转页面
                .failureHandler(myFailureHandler)
                .permitAll();

        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
//        http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class);
//        http.addFilterAfter(jwtSuccessTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }


    //AuthenticatManager的配置
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder());
    }

   /* @Override
    public void configure(WebSecurity web) throws Exception {
        //解决静态资源被拦截的问题
        web.ignoring().antMatchers("/static/**");
    }*/

    @Bean
    public PasswordEncoder passwordEncoder() {

        return new PasswordEncoderImpl();
    }

}
